//
//  RailTransportDao.m
//  CJRailTransport
//
//  Created by Hank on 2018/1/3.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "RailTransportDao.h"
#import "BinningWestModel.h"

@implementation RailTransportDao
// 添加西向装车
+ (BOOL)addBinningWest:(BinningWestModel *)binningWestModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"insert into BinningWest(containerNo, carVIN, stentsNo ,trayNo , stentsGPSNo, trayGPSNo, putSequence, directionNo, date, location, supplier, userId, createTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
    
    [db open];
    
    BOOL result;
    result = [db executeUpdate:sql,
              binningWestModel.containerNo,
              binningWestModel.carVIN,
              binningWestModel.stentsNo,
              binningWestModel.trayNo,
              binningWestModel.stentsGPSNo,
              binningWestModel.trayGPSNo,
              binningWestModel.putSequence,
              binningWestModel.directionNo,
              binningWestModel.date,
              binningWestModel.location,
              binningWestModel.supplier,
              binningWestModel.userId,
              binningWestModel.createTime];
    [db close];
    
    return result;
}

// 添加东向装车
+ (BOOL)addBinningEast:(BinningEastModel *)binningEastModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"insert into BinningEast(containerNo, carVIN, leftStentsNo ,rightStentsNo, leftStentsGPSNo,  rightStentsGPSNo, putSequence, directionNo, date, location, supplier, userId, createTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
    
    [db open];
    
    BOOL result;
    result = [db executeUpdate:sql,
              binningEastModel.containerNo,
              binningEastModel.carVIN,
              binningEastModel.leftStentsNo,
              binningEastModel.rightStentsNo,
              binningEastModel.leftStentsGPSNo,
              binningEastModel.rightStentsGPSNo,
              binningEastModel.putSequence,
              binningEastModel.directionNo,
              binningEastModel.date,
              binningEastModel.location,
              binningEastModel.supplier,
              binningEastModel.userId,
              binningEastModel.createTime];
    [db close];
    
    return result;
}

// 添加支架装车
+ (BOOL)addBinningBracket:(BinningBracketModel *)binningBracketModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"insert into BinningBracket(containerNo, stentsOrTrayNo, gpsNo, directionNo, date, location, supplier, userId, createTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
    
    [db open];
    
    BOOL result;
    result = [db executeUpdate:sql,
              binningBracketModel.containerNo,
              binningBracketModel.stentsOrTrayNo,
              binningBracketModel.gpsNo,
              binningBracketModel.directionNo,
              binningBracketModel.date,
              binningBracketModel.location,
              binningBracketModel.supplier,
              binningBracketModel.userId,
              binningBracketModel.createTime];
    [db close];
    
    return result;
}

// 查询西向装车数据
+ (NSMutableArray *)selectBinningWest:(NSString *)userId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    
    NSString *sql = [NSString stringWithFormat: @"select westId, containerNo, carVIN, stentsNo, trayNo, stentsGPSNo, trayGPSNo, putSequence, directionNo, date, location, supplier, userId, createTime from BinningWest where userId = '%@' order by createTime desc",userId];
    
    [db open];
    
    NSMutableArray *binningWestList = [[NSMutableArray alloc] init];
    
    FMResultSet *results = [db executeQuery:sql];
    while( [results next] ) {
        BinningWestModel *binningWestModel = [[BinningWestModel alloc] init];
        binningWestModel.westId = [results intForColumnIndex:0];
        binningWestModel.containerNo = [results stringForColumnIndex:1];
        binningWestModel.carVIN = [results stringForColumnIndex:2];
        binningWestModel.stentsNo = [results stringForColumnIndex:3];
        binningWestModel.trayNo = [results stringForColumnIndex:4];
        binningWestModel.stentsGPSNo = [results stringForColumnIndex:5];
        binningWestModel.trayGPSNo = [results stringForColumnIndex:6];
        binningWestModel.putSequence = [results stringForColumnIndex:7];
        binningWestModel.directionNo = [results stringForColumnIndex:8];
        binningWestModel.date = [results stringForColumnIndex:9];
        binningWestModel.location = [results stringForColumnIndex:10];
        binningWestModel.supplier = [results stringForColumnIndex:11];
        binningWestModel.userId = [results stringForColumnIndex:12];
        binningWestModel.createTime = [results stringForColumnIndex:13];
        [binningWestList addObject:binningWestModel];
    }
    
    [db close];
    return binningWestList;
}

// 查询东向装车数据
+ (NSMutableArray *)selectBinningEast:(NSString *)userId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    
    NSString *sql = [NSString stringWithFormat: @"select eastId, containerNo, carVIN, leftStentsNo, rightStentsNo, leftStentsGPSNo, rightStentsGPSNo, putSequence, directionNo, date, location, supplier, userId, createTime from BinningEast where userId = '%@' order by createTime desc",userId];
    
    [db open];
    
    NSMutableArray *binningEastList = [[NSMutableArray alloc] init];
    
    FMResultSet *results = [db executeQuery:sql];
    while( [results next] ) {
        BinningEastModel *binningEastModel = [[BinningEastModel alloc] init];
        binningEastModel.eastId = [results intForColumnIndex:0];
        binningEastModel.containerNo = [results stringForColumnIndex:1];
        binningEastModel.carVIN = [results stringForColumnIndex:2];
        binningEastModel.leftStentsNo = [results stringForColumnIndex:3];
        binningEastModel.rightStentsNo = [results stringForColumnIndex:4];
        binningEastModel.leftStentsGPSNo = [results stringForColumnIndex:5];
        binningEastModel.rightStentsGPSNo = [results stringForColumnIndex:6];
        binningEastModel.putSequence = [results stringForColumnIndex:7];
        binningEastModel.directionNo = [results stringForColumnIndex:8];
        binningEastModel.date = [results stringForColumnIndex:9];
        binningEastModel.location = [results stringForColumnIndex:10];
        binningEastModel.supplier = [results stringForColumnIndex:11];
        binningEastModel.userId = [results stringForColumnIndex:12];
        binningEastModel.createTime = [results stringForColumnIndex:13];
        [binningEastList addObject:binningEastModel];
    }
    
    [db close];
    return binningEastList;
}

// 查询支架或托盘装车数据
+ (NSMutableArray *)selectBinningBracket:(NSString *)userId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    
    NSString *sql = [NSString stringWithFormat: @"select bracketId, containerNo, stentsOrTrayNo, gpsNo, directionNo, date, location, supplier, userId, createTime from BinningBracket where userId = '%@' order by createTime desc",userId];
    
    [db open];
    
    NSMutableArray *binningBracketList = [[NSMutableArray alloc] init];
    
    FMResultSet *results = [db executeQuery:sql];
    while( [results next] ) {
        BinningBracketModel *binningBracketModel = [[BinningBracketModel alloc] init];
        binningBracketModel.bracketId = [results intForColumnIndex:0];
        binningBracketModel.containerNo = [results stringForColumnIndex:1];
        binningBracketModel.stentsOrTrayNo = [results stringForColumnIndex:2];
        binningBracketModel.gpsNo = [results stringForColumnIndex:3];
        binningBracketModel.directionNo = [results stringForColumnIndex:4];
        binningBracketModel.date = [results stringForColumnIndex:5];
        binningBracketModel.location = [results stringForColumnIndex:6];
        binningBracketModel.supplier = [results stringForColumnIndex:7];
        binningBracketModel.userId = [results stringForColumnIndex:8];
        binningBracketModel.createTime = [results stringForColumnIndex:9];
        [binningBracketList addObject:binningBracketModel];
    }
    
    [db close];
    return binningBracketList;
}

// 删除西向装车数据
+ (BOOL)deleteBinningWest:(NSInteger)westId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"delete from BinningWest where westId = ?";
    
    BOOL excuteResult;
    [db open];
    excuteResult = [db executeUpdate:sql,@(westId)];
    
    [db close];
    
    return excuteResult;
}

// 删除东向装车数据
+ (BOOL)deleteBinningEast:(NSInteger)eastId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"delete from BinningEast where eastId = ?";
    
    BOOL excuteResult;
    [db open];
    excuteResult = [db executeUpdate:sql,@(eastId)];
    
    [db close];
    
    return excuteResult;
}

// 删除支架或托盘装车数据
+ (BOOL)deleteBinningBracket:(NSInteger)bracketId
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"delete from BinningBracket where bracketId = ?";
    
    BOOL excuteResult;
    [db open];
    excuteResult = [db executeUpdate:sql,@(bracketId)];
    
    [db close];
    
    return excuteResult;
}

// 更新西向装车
+ (BOOL)updateBinningWest:(BinningWestModel *)binningWestModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"update BinningWest set containerNo = ?, carVIN = ?, stentsNo = ?, trayNo = ?, stentsGPSNo = ?, trayGPSNo = ?, putSequence = ?, createTime = ? where westId = ?";
    
    [db open];
    
    BOOL result = [db executeUpdate:sql,binningWestModel.containerNo,
        binningWestModel.carVIN,
        binningWestModel.stentsNo,
        binningWestModel.trayNo,
        binningWestModel.stentsGPSNo,
        binningWestModel.trayGPSNo,
        binningWestModel.putSequence,
        binningWestModel.createTime,
        @(binningWestModel.westId)];
    
    [db close];
    
    return result;
}

// 更新东向装车
+ (BOOL)updateBinningEast:(BinningEastModel *)binningEastModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"update BinningEast set containerNo = ?, carVIN = ?, leftStentsNo = ?, rightStentsNo = ?, leftStentsGPSNo = ?, rightStentsGPSNo = ?, putSequence = ?, createTime = ? where eastId = ?";
    
    [db open];
    
    BOOL result = [db executeUpdate:sql,binningEastModel.containerNo,
        binningEastModel.carVIN,
        binningEastModel.leftStentsNo,
        binningEastModel.rightStentsNo,
        binningEastModel.leftStentsGPSNo,
        binningEastModel.rightStentsGPSNo,
        binningEastModel.putSequence,
        binningEastModel.createTime,
        @(binningEastModel.eastId)];
    
    [db close];
    
    return result;
}

// 更新支架或托盘装车
+ (BOOL)updateBinningBracket:(BinningBracketModel *)binningBracketModel
{
    NSString *dbPath = [FMDatabaseEx getDbPath];
    
    FMDatabaseEx *db  = [FMDatabaseEx databaseWithPath:dbPath];
    NSString *sql = @"update BinningBracket set containerNo = ?, stentsOrTrayNo = ?, gpsNo = ?, createTime = ? where bracketId = ?";
    
    [db open];
    
    BOOL result = [db executeUpdate:sql,binningBracketModel.containerNo,
        binningBracketModel.stentsOrTrayNo,
        binningBracketModel.gpsNo,
        binningBracketModel.createTime,
        @(binningBracketModel.bracketId)];
    
    [db close];
    
    return result;
}

@end
